package kr.saeildadim.androidexam_saeildamil.chapter16;

import java.util.Calendar;

import kr.saeildadim.androidexam_saeildamil.R;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

public class Ch16_OkCancel extends Activity {
	final static int SAMPLEDIALOG =0;
	final static int QUESTIONDIALOG =1;
	int a = 3;
	int b =4;
	int result;
	int mSelect =0;
	boolean[] mSel ={false,false,false,false}; 
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ch16_dialogtest);
		
	}
	
	public void mOnClick(View v){
		switch(v.getId()){
		case R.id.ch16_dialogtext_btn01:
			AlertDialog.Builder bd = new AlertDialog.Builder(this);
			bd.setTitle("알립니다");
			bd.setMessage("대화상자를 열었습니다.");
			bd.setIcon(R.drawable.huysung);
			bd.show();
			break;
		case R.id.ch16_dialogtext_btn02:
			new AlertDialog.Builder(this)
			.setTitle("알립니다")
			.setMessage("대화상자를 열었습니다")
			.setIcon(R.drawable.huysung)
			.setNegativeButton("닫기", null)
			.setCancelable(false)
			.show();
			break;
		case R.id.ch16_dialogtext_btn03:
			new AlertDialog.Builder(this)
			.setTitle("질문")
			.setMessage("기부하시겠습니까?")
			.setIcon(R.drawable.huysung)
			.setPositiveButton("pos", null)
			.setNeutralButton("net", null)
			.setNegativeButton("nag", null)
			.setCancelable(false)
			.show();
			break;
		case R.id.ch16_dialogtext_btn04:
			showDialog(SAMPLEDIALOG);
			break;
		case R.id.ch16_dialogtext_btn05:
			showDialog(QUESTIONDIALOG);
			break;
		case R.id.ch16_dialogtext_btn06:
			showModal();
			break;
		case R.id.ch16_dialogtext_btn07:
			new AlertDialog.Builder(this)
			.setTitle("질문")
			.setMessage("어떤연산을 하시겠습니까?")
			.setPositiveButton("덧셈", listener)
			.setNegativeButton("곱셈", listener)
			.show();
			break;
		case R.id.ch16_dialogtext_btn08:
			new AlertDialog.Builder(this)
			.setTitle("음식을 선택하시오")
			.setIcon(R.drawable.ic_launcher)
			.setSingleChoiceItems(R.array.foods, mSelect, new DialogInterface.OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					// TODO Auto-generated method stub
					mSelect = which;
				}
			})
			.setPositiveButton("확인", new DialogInterface.OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					String[] foods =getResources().getStringArray(R.array.foods);
					TextView text = (TextView) findViewById(R.id.ch16_dialigtest_tv3);
					text.setText("선택한 음식 = "+foods[mSelect]);
					
				}
			})
			.setNegativeButton("취소", null)
			.show();
			
			
			break;
		case R.id.ch16_dialogtext_btn09:
			new AlertDialog.Builder(this)
			.setTitle("음식을 선택하세요")
			.setIcon(R.drawable.ic_launcher)
			.setMultiChoiceItems(R.array.foods, mSel, new DialogInterface.OnMultiChoiceClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which, boolean isChecked) {
					mSel[which] = isChecked;
					
				}
			})
			.setPositiveButton("확인", new DialogInterface.OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					String[] foods = getResources().getStringArray(R.array.foods);
					TextView text = (TextView) findViewById(R.id.ch16_dialigtest_tv4);
					String result = "선택한 음식 =";
					for(int i =0;i<mSel.length;i++){
						if(mSel[i]){
							result += foods[i]+" ";
						}
					}
					text.setText(result);
				}
			})
			.setNegativeButton("취소", null)
			.show();
			break;
		}
		
		
	}//mOnClick
	DialogInterface.OnClickListener listener = new OnClickListener() {
		
		@Override
		public void onClick(DialogInterface dialog, int which) {
			
			if(which == DialogInterface.BUTTON_POSITIVE){
				result = a + b;
			}else{
				result = a * b;
			}
			TextView text = (TextView) findViewById(R.id.ch16_dialigtest_tv2);
			text.setText("연산결과  = "+result);
			Toast.makeText(Ch16_OkCancel.this, "연산 완료", Toast.LENGTH_LONG).show();
			
		}
	};
	private void showModal(){
		try{
			Thread.sleep(3000);
		}catch(Exception e){}
		Boolean stat = true;
		if(stat){
			new AlertDialog.Builder(this)
			.setTitle("예외발생")
			.setMessage("해당 이유로 에러발생")
			.setPositiveButton("종료", new OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					finish();
					
				}
			})
			.show();
		}else{
		Toast.makeText(this, "정상완료", Toast.LENGTH_LONG).show();
		}
	}

	@Override
	@Deprecated
	protected Dialog onCreateDialog(int id) {
		if(id == SAMPLEDIALOG){
			return new AlertDialog.Builder(this)
			.setTitle("알림")
			.setMessage("대화상자오픈")
			.setIcon(R.drawable.huysung)
			.setPositiveButton("닫기", null)
			.setCancelable(false)
			.create();
		}else{
			return new AlertDialog.Builder(this)
			.setTitle("질문")
			.setMessage("밥먹었나요")
			.setPositiveButton("먹었음", null)
			.setNegativeButton("굶었음", null)
			.create();
		}
	}

	@Override
	@Deprecated
	protected void onPrepareDialog(int id, Dialog dialog) {
		// TODO Auto-generated method stub
		super.onPrepareDialog(id, dialog);
	switch(id){
		case SAMPLEDIALOG:
			break;
		case QUESTIONDIALOG:
			Calendar cal = Calendar.getInstance();
			String sTime = String.format("%d시 %d분 %d초", 
					cal.get(Calendar.HOUR),
					cal.get(Calendar.MINUTE),
					cal.get(Calendar.SECOND));
			dialog.setTitle(sTime);
			break;
	}
		
}
	
	
}
